package top.infopub.mgr.mkt.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.infopub.enums.mkt.ProProcessDetailTypeEnum;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.service.ProcessNodeService;
import top.infopub.mgr.flow.service.ProcessService;
import top.infopub.mgr.flow.service.TemplateNodeService;
import top.infopub.mgr.flow.vo.ProcessNodeVo;
import top.infopub.mgr.mkt.domain.ProjectApprovalDomain;
import top.infopub.mgr.mkt.service.ProProcessManageService;
import top.infopub.mgr.mkt.service.ProjectApprovalService;
import top.infopub.model.flow.Process;
import top.infopub.model.flow.TemplateNode;
import top.infopub.model.mkt.ProProcessDetail;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/historyProcess")
public class HistoryProcessController {

    @Autowired
    private ProjectApprovalService projectApprovalService;

    @Autowired
    private ProcessService processService;

    @Autowired
    private ProcessNodeService processNodeService;

    @Autowired
    private TemplateNodeService templateNodeService;

    @Autowired
    private ProProcessManageService proProcessManageService;

    @RequestMapping("/list")
    @ResponseBody
    public RspVo list(String proCode, ParamPage paramPage) {
        Page<ProjectApprovalDomain> page = new Page<ProjectApprovalDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        page= projectApprovalService.queryProjectApproval(page,proCode);
        return RspVo.getSuccessResponse(page.getTotal(),page.getResult());
    }

    @RequestMapping("/processList")
    @ResponseBody
    public RspVo processList(String processId) {
        Process proc = processService.selectByPrimaryKey(processId);
        if (proc == null) return RspVo.getFailureResponse(400, "流程信息不存在");
        List<ProcessNodeVo> procNodeList = processNodeService.queryAllProcNodes(processId);
        List<TemplateNode> tempNodeList = templateNodeService.queryAllByTemplateId(proc.getTemplateId());
        Map<String, Object> map = new HashMap<>();
        map.put("procNodeList", procNodeList);
        map.put("tempNodeList", tempNodeList);
        return RspVo.getSuccessResponse(map);
    }

    @RequestMapping("/toDetail")
    public String toDetail(HttpServletRequest request, String processId) {
        request.setAttribute("processId", processId);
        ProProcessDetail ppd = proProcessManageService.queryByProcessId(processId);
        if (ppd == null || StringUtils.isBlank(ppd.getDetailJson())) {
            return null;
        }
        request.setAttribute("nodeType", ppd.getProFlowNode());
        if (StringUtils.equals(ProProcessDetailTypeEnum.PRO_NODE_TYPE0.getNode(), ppd.getProFlowNode())) {
            return "mkt/procdetail/lay-collectProject-process";
        } else if (StringUtils.equals(ProProcessDetailTypeEnum.PRO_NODE_TYPE1.getNode(), ppd.getProFlowNode())) {
            return "mkt/procdetail/lay-preProject-process";
        } else if (StringUtils.equals(ProProcessDetailTypeEnum.PRO_NODE_TYPE2.getNode(), ppd.getProFlowNode())) {
            return "mkt/procdetail/lay-projectApproval-process";
        } else if (StringUtils.equals(ProProcessDetailTypeEnum.PRO_NODE_TYPE3.getNode(), ppd.getProFlowNode())) {
            return "mkt/procdetail/lay-marketingPlanning-process";
        } else if (StringUtils.equals(ProProcessDetailTypeEnum.PRO_NODE_TYPE4.getNode(), ppd.getProFlowNode())) {
            return "mkt/procdetail/lay-prequalification-process";
        } else if (StringUtils.equals(ProProcessDetailTypeEnum.PRO_NODE_TYPE5.getNode(), ppd.getProFlowNode())) {
            return "mkt/procdetail/lay-biddingDocuments-process";
        } else if (StringUtils.equals(ProProcessDetailTypeEnum.PRO_NODE_TYPE6.getNode(), ppd.getProFlowNode())) {
            return "mkt/procdetail/lay-tenderPlanning-process";
        } else if (StringUtils.equals(ProProcessDetailTypeEnum.PRO_NODE_TYPE7.getNode(), ppd.getProFlowNode())) {
            return "mkt/procdetail/lay-biddingDecision-process";
        } else if (StringUtils.equals(ProProcessDetailTypeEnum.PRO_NODE_TYPE8.getNode(), ppd.getProFlowNode())) {
            return "mkt/procdetail/lay-marginPayment-process";
        } else if (StringUtils.equals(ProProcessDetailTypeEnum.PRO_NODE_TYPE9.getNode(), ppd.getProFlowNode())) {
            return "mkt/procdetail/lay-tenderReview-businessStandard-process";
        } else if (StringUtils.equals(ProProcessDetailTypeEnum.PRO_NODE_TYPE10.getNode(), ppd.getProFlowNode())) {
            return "mkt/procdetail/lay-bidOpeningResult-process";
        } else if (StringUtils.equals(ProProcessDetailTypeEnum.PRO_NODE_TYPE11.getNode(), ppd.getProFlowNode())) {
            return "mkt/procdetail/lay-summaryAfterBidding-process";
        } else if (StringUtils.equals(ProProcessDetailTypeEnum.PRO_NODE_TYPE12.getNode(), ppd.getProFlowNode())) {
            return "mkt/procdetail/lay-confess-process";
        } else if (StringUtils.equals(ProProcessDetailTypeEnum.PRO_NODE_TYPE13.getNode(), ppd.getProFlowNode())) {
            return "mkt/procdetail/lay-tenderSubsidy-process";
        } else if (StringUtils.equals(ProProcessDetailTypeEnum.PRO_NODE_TYPE14.getNode(), ppd.getProFlowNode())
                || StringUtils.equals(ProProcessDetailTypeEnum.PRO_NODE_TYPE17.getNode(), ppd.getProFlowNode())) {
            return "mkt/procdetail/lay-marginRecovery-process";
        } else if (StringUtils.equals(ProProcessDetailTypeEnum.PRO_NODE_TYPE15.getNode(), ppd.getProFlowNode())) {
            return "mkt/procdetail/lay-tenderReview-beacon-process";
        } else if (StringUtils.equals(ProProcessDetailTypeEnum.PRO_NODE_TYPE16.getNode(), ppd.getProFlowNode())) {
            return "mkt/procdetail/lay-tenderReview-technicalProposal-process";
        } else if (StringUtils.equals(ProProcessDetailTypeEnum.PRO_NODE_TYPE18.getNode(), ppd.getProFlowNode())) {
            return "mkt/procdetail/lay-projectTracking-process";
        } else if (StringUtils.equals(ProProcessDetailTypeEnum.PRO_NODE_TYPE19.getNode(), ppd.getProFlowNode())) {
            return "mkt/procdetail/lay-marketingRewards-process";
        }
        return null;
    }

    @RequestMapping("/proNodeDetail")
    @ResponseBody
    public RspVo proNodeList(String processId) {
        Process proc = processService.selectByPrimaryKey(processId);
        if (proc == null) return RspVo.getFailureResponse(400, "流程信息不存在");
        ProProcessDetail ppd = proProcessManageService.queryByProcessId(processId);
        if (ppd == null || StringUtils.isBlank(ppd.getDetailJson())) return RspVo.getFailureResponse(400, "流程详情信息不存在");
        List<ProcessNodeVo> procNodeList = processNodeService.queryAllProcNodes(processId);
        List<TemplateNode> tempNodeList = templateNodeService.queryAllByTemplateId(proc.getTemplateId());
        Map<String, Object> map = new HashMap<>();
        JSONObject json = JSON.parseObject(StringEscapeUtils.unescapeJava(ppd.getDetailJson()));
        map.put("procNodeList", procNodeList);
        map.put("tempNodeList", tempNodeList);
        map.put("detailJson", json);
        return RspVo.getSuccessResponse(map);
    }
}
